package com.kzd.btgame.main.access

import android.text.method.LinkMovementMethod
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.core.widget.addTextChangedListener
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.lifecycleScope
import com.blankj.utilcode.util.RegexUtils
import com.kzd.btgame.R
import com.kzd.btgame.custom.setOnClickAntiShakeListener
import com.kzd.btgame.custom.toAgreeLinks
import com.kzd.btgame.databinding.FragmentAccessLoginBinding
import com.kzd.btgame.jetpack.vm.AccessViewModel
import com.kzd.btgame.utils.countdown
import com.module.lemlin.http.Status
import com.module.lemlin.owner.OwnerViewBindingFragment
import com.module.lemlin.owner.ownerViewModel
import kotlinx.coroutines.Job

class AccessLoginFragment : OwnerViewBindingFragment<FragmentAccessLoginBinding>() {

    companion object {
        fun newInstance(): AccessLoginFragment {
            return AccessLoginFragment()
        }
    }

    private var mCountdownJob: Job? = null

    private val mViewModel: AccessViewModel by lazy {
        ownerViewModel(requireActivity())
    }

    override val inflate: (LayoutInflater, ViewGroup?, Boolean) -> FragmentAccessLoginBinding
        get() = FragmentAccessLoginBinding::inflate

    override fun initView() {
        mViewBinding.tvAccessLoginPact.text =
            resources.getString(R.string.app_pact1).toAgreeLinks(activity = requireActivity())
        mViewBinding.tvAccessLoginPact.movementMethod = LinkMovementMethod.getInstance()

        mViewBinding.etAccessLoginAccount.addTextChangedListener {
            mViewBinding.tvAccessLoginSubmit.isEnabled = !it.isNullOrEmpty() &&
                    !mViewBinding.etAccessLoginVerifyCode.text.isNullOrEmpty()
            mViewBinding.tvAccessLoginGetCode.isEnabled = it?.length == 11
                    && RegexUtils.isMobileExact(it)
            if (it?.length == 11 && !RegexUtils.isMobileExact(it)) {
                toast("请输入正确的手机号")
            }
        }

        mViewBinding.etAccessLoginVerifyCode.addTextChangedListener {
            mViewBinding.tvAccessLoginSubmit.isEnabled = !it.isNullOrEmpty() &&
                    !mViewBinding.etAccessLoginAccount.text.isNullOrEmpty()
        }

        mViewBinding.tvAccessLoginGetCode.setOnClickAntiShakeListener {
            mViewModel.accessGetCode(mViewBinding.etAccessLoginAccount.text.toString(), type = 3)
        }

        mViewBinding.tvAccessLoginSubmit.setOnClickAntiShakeListener {
            mViewModel.accessLoginVerify(
                mViewBinding.etAccessLoginAccount.text.toString(),
                mViewBinding.etAccessLoginVerifyCode.text.toString()
            )
        }
    }

    override fun initViewMode() {
        mViewModel.accessGetCodeLiveData.observe(this as LifecycleOwner, { response ->
            if (response == null) {
                return@observe
            }
            if (response.status != Status.SUCCESS)
                return@observe
            mCountdownJob = countdown(
                total = 60, scope = lifecycleScope, onTick = { second ->
                    mViewBinding.tvAccessLoginGetCode.text = "剩${second}秒"
                }, onStart = {
                    mViewBinding.tvAccessLoginGetCode.isEnabled = false
                }, onFinish = {
                    mViewBinding.tvAccessLoginGetCode.text = "发送验证码"
                    mViewBinding.tvAccessLoginGetCode.isEnabled =
                        !mViewBinding.etAccessLoginAccount.text.isNullOrEmpty()
                    mCountdownJob?.cancel()
                }
            )
        })
    }

    override fun onDestroy() {
        super.onDestroy()
        mCountdownJob?.cancel()
        mViewModel.accessGetCodeLiveData.postValue(null)
    }

}